<?php


App::uses('Crud', 'Model');

class AppliedQuestion extends Crud
{
    const SKIPPED_QUESTION = 1;
    const NOT_SKIPPED_QUESTION = 0;
    
    
    public function skipQuestion( $id )
    {
        $this->update($id,array('skipped'=>self::SKIPPED_QUESTION));
    }
    
    public function nextQuestion( $examId, $answered )
    {
        $question = array('id'=>0);
                
        $data = array(
            'conditions' => array(
                'exam_id' => $examId
            )
        );
        if(count($answered))
        {
            array_push($answered,'1');
            $data['conditions']['NOT'] = array(
                'AppliedQuestion.id' => $answered
            );
        }
        
        if( $this->find('count', $data ) > 0 )
        {
            $data['conditions']['skipped'] = self::NOT_SKIPPED_QUESTION;
            if( $this->find('count', $data ) == 0 )
            {
                $this->updateAll( array( 'skipped'=> self::NOT_SKIPPED_QUESTION ),
                                  array( 'exam_id' => $examId ) );
            }
            //$data['conditions']['skippeds'] = 0;
            $resQuestion = $this->find('first', $data);
            $question = $resQuestion['AppliedQuestion'];
        }
        return $question;
    }
    
}